import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;

public class Bag<Item> implements Iterable<Item> {
    private Node first;
    private class Node
    {
        Item item;
        Node next;
    }
    public void add(Item item){
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }


    public Iterator<Item> iterator() {
        return new ListIterator();
    }
    private class ListIterator implements Iterator<Item>{
        private Node current = first;


        public boolean hasNext() {
            return current != null;
        }


        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }


        public void remove() {

        }
    }

    public  static void main(String[] args){
        Bag<String> s = new Bag<String>();
        while(!StdIn.isEmpty()){
            String item = StdIn.readString();
            if(!item.equals("-")){
                s.add(item);
            }
//            else if(!s.isEmpty()){
//                StdOut.println(s.pop()+" ");
//            }
        }
//        while(!s.isEmpty()){
//            StdOut.println(s.pop());
//        }
        Iterator<String> i = s.iterator();
        while(i.hasNext()) {
            String st = i.next();
            StdOut.println(st);
        }
//        StdOut.println("("+s.size() + "left on stack");
    }
}
